﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using HybridLab.Workflow.Activities;
using HybridLab.Workflow.Activities.Info;
using HybridLab.Virtual.Interfaces;
using Microsoft.TeamFoundation.Build.Client;

namespace HybridLab.Workflow.Activities.CodeActivities
{
	[BuildActivity(HostEnvironmentOption.All)]
	public sealed class RestoreVMSnapshotActivity : CodeActivity
	{
		public InArgument<IVirtualMachine> Machine { get; set; }
		public InArgument<string> SnapshotName { get; set; }

		protected override void Execute(CodeActivityContext context)
		{
			var machine = context.GetValue(Machine);
			var snapshotName = context.GetValue(SnapshotName);
				
			if (!machine.Snapshots.Any(s => s.Equals(snapshotName, StringComparison.OrdinalIgnoreCase)))
			{
				context.LogError("Cannot find snapshot {0} on machine {1}", snapshotName, machine.Name);
			}
			else
			{
				context.LogMessage("Restoring snapshot {0} for machine {1}", snapshotName, machine.Name);
				machine.ApplySnapshot(snapshotName);
			}
		}
	}
}
